<?php
// redis冷数据写入MySQL中

require_once '../lib.php';
$redis = redis_conn();
$i = 0;
$sql = 'INSERT INTO `post` (`postid`, `userid`, `username`, `time`, `content`) VALUES ';
//查询数量
while ( $redis->lLen('global:store') > 0 && $i++ < 1000) {
    //1.弹出global:store 中的一个
    $postid = $redis->rPop('global:store');
    //2.删除对应的微博del
    $rs = $redis->delete('post:postid:'.$postid);
    //$postid = 7;
    $post = $redis->hMGet('post:postid:'.$postid, ['userid', 'username', 'time', 'content']);
    $sql .= "($postid, $post[userid], '$post[username]', '$post[time]', '$post[content]'),";
}
$sql = rtrim($sql,',');
echo $sql;

if($i == 0) {
    exit('no job');
}
//mysql连接
$conn = mysqli_connect('127.0.0.1', 'root', 'root', 'test', '3306');
mysqli_query($conn, 'set names utf8');

$rs = mysqli_query($conn, $sql);

if($rs) {
    echo 'ok';
}else {
    echo mysqli_error($conn);
}
mysqli_close($conn);
